<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestInternalMail extends PHPUnit_Framework_TestCase {
	
	public function test_send_mail_from_not_set() {
		$mail = new InternalMail();
		
		$error_nav = $this->getMock('ErrorNavigation');

		$error_nav->expects($this->once())
				  ->method('errorpage')
				  ->with(9);


		$mail->set_error_navigation($error_nav);

		$mail->send_mail();
	}

	public function test_send_mail_to_not_set() {
		$mail = new InternalMail();

		$error_nav = $this->getMock('ErrorNavigation');

		$error_nav->expects($this->never())
				  ->method('errorpage');

		$db_access = $this->getMock('DbAccessor');

		$db_access->expects($this->never())
				  ->method('db_sqlize');

		$db_access->expects($this->never())
				  ->method('db_query_insert');

		$db_access->expects($this->never())
				  ->method('db_do_query');

		$db_access->expects($this->never())
				  ->method('db_insert_id');

		$mail->set_db_accessor($db_access);

		$alerter = $this->getMock('Alerter');
		$alerter->expects($this->once(1))
				->method('add_alert')
				->with('Je selecteerde geen leden om de mail naar te sturen.');

		$mail->from = "TestUser";

		$mail->set_alerter($alerter);

		$mail->send_mail();
	}

	public function test_send_mail_doesnt_validate() {
		$mail = new InternalMail();

		$db_access = $this->getMock('DbAccessor');

		$db_access->expects($this->never())
				  ->method('db_sqlize');

		$db_access->expects($this->never())
				  ->method('db_query_insert');

		$db_access->expects($this->never())
				  ->method('db_do_query');

		$db_access->expects($this->never())
				  ->method('db_insert_id');

		$checker = $this->getMock('InputChecker');
		$checker->expects($this->once())
				->method('check_mail_input')
				->will($this->returnValue(false));

		$alerter = $this->getMock('Alerter');
		$alerter->expects($this->once(1))
				->method('add_alert')
				->with('De tekst kon niet gevalideerd worden.');

		$address_translator = $this->getMock('AddressTranslator');
		$address_translator->expects($this->once())
						   ->method('translate_addresses_to_db_keys')
						   ->with(0)
						   ->will($this->returnValue(array(1, 2, 3)));

		$mail->set_db_accessor($db_access);
		$mail->set_input_checker($checker);
		$mail->set_alerter($alerter);
		$mail->set_address_translator($address_translator);

		$mail->from = "TestUser";
		$mail->to = "TestRecipient";

		$mail->send_mail();
	}

	public function test_send_mail_valid() {
		$mail = new InternalMail();
		$expectedId = 5;

		$db_access = $this->getMock('DbAccessor');

		$db_access->expects($this->exactly(3))
				  ->method('db_sqlize')
				  ->will($this->returnArgument(0));

		$db_access->expects($this->exactly(2))
				  ->method('db_query_insert')
				  ->will($this->returnValue("test"));
		$db_access->expects($this->exactly(1))
				  ->method('db_query_insert_multiple')
				  ->will($this->returnValue("test"));

		$db_access->expects($this->exactly(3))
				  ->method('db_do_query');

		$db_access->expects($this->once())
				  ->method('db_insert_id')
				  ->will($this->returnValue($expectedId));

		$checker = $this->getMock('InputChecker');
		$checker->expects($this->once())
				->method('check_mail_input')
				->will($this->returnValue(true));

		$alerter = $this->getMock('Alerter');
		$alerter->expects($this->never())
				->method('add_alert');

		$address_translator = $this->getMock('AddressTranslator');
		$address_translator->expects($this->once())
						   ->method('translate_addresses_to_db_keys')
						   ->with(0)
						   ->will($this->returnValue(array(1, 2, 3)));

		$mail->set_db_accessor($db_access);
		$mail->set_input_checker($checker);
		$mail->set_alerter($alerter);
		$mail->set_address_translator($address_translator);

		$mail->from = "TestUser";
		$mail->to = "TestRecipient";
		$mail->icon = "TestIcon";
		$mail->subject = "TestSubject";

		$mail->send_mail();
	}

	public function test_send_mail_valid_with_cc_keys() {
		$mail = new InternalMail();
		$expectedId = 5;
		$expected_cc_keys = array(123);

		$db_access = $this->getMock('DbAccessor');

		$db_access->expects($this->exactly(3))
				  ->method('db_sqlize')
				  ->will($this->returnArgument(0));

		$db_access->expects($this->exactly(2))
				  ->method('db_query_insert')
				  ->will($this->returnValue("test"));
		$db_access->expects($this->exactly(2))
				  ->method('db_query_insert_multiple')
				  ->will($this->returnValue("test"));

		$db_access->expects($this->exactly(4))
				  ->method('db_do_query');

		$db_access->expects($this->once())
				  ->method('db_insert_id')
				  ->will($this->returnValue($expectedId));

		$checker = $this->getMock('InputChecker');
		$checker->expects($this->once())
				->method('check_mail_input')
				->will($this->returnValue(true));

		$alerter = $this->getMock('Alerter');
		$alerter->expects($this->never())
				->method('add_alert');

		$address_translator = $this->getMock('AddressTranslator');
		$address_translator->expects($this->once())
						   ->method('translate_addresses_to_db_keys')
						   ->with(0)
						   ->will($this->returnValue(array(1, 2, 3)));

		$mail->set_db_accessor($db_access);
		$mail->set_input_checker($checker);
		$mail->set_alerter($alerter);
		$mail->set_address_translator($address_translator);
		$mail->set_cc_keys($expected_cc_keys);

		$mail->from = "TestUser";
		$mail->to = "TestRecipient";
		$mail->icon = "TestIcon";
		$mail->subject = "TestSubject";

		$mail->send_mail();
	}
}
